package com.tarena.lbs.user.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.AdminService;
import com.tarena.lbs.user.service.UserGroupService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserGroupServiceImpl implements UserGroupService {
    private static final Logger log = LoggerFactory.getLogger(UserGroupServiceImpl.class);
    @Autowired
    private AdminServiceImpl adminServiceImpl;
    @Autowired
    private UserGroupRepository userGroupRepository;
    @Override
    public PageResult<UserGroupVO> pageList(UserGroupQuery query) throws BusinessException {// 分页查询用户人群列表
        //获取用户id
        AdminVO adminVO = adminServiceImpl.detail();
        //断言异常
        Asserts.isTrue(adminVO == null, new BusinessException("-2", "认证解析错误"));
        //得到商家id
        Integer businessId = adminVO.getBusinessId();
        //判断商家id是否为空
        Asserts.isTrue(businessId == null, new BusinessException("-2", "商家id不能为空"));
        query.setBusinessId(businessId);
        //通过商家id获取用户分组列表
        PageResult<UserGroupVO> pageResult = new PageResult<>();
        PageInfo<UserGroupPO> pageInfo = userGroupRepository.pagesList(query);
        pageResult.setPageNo(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        // 将分页信息中的总记录数设置到分页结果对象中
        pageResult.setTotal(pageInfo.getTotal());
        // 声明一个存储管理员视图对象的列表
        List<UserGroupVO> vos = null;
        // 从分页信息中获取用户人群持久化对象列表
        List<UserGroupPO> pos = pageInfo.getList();
        // 判断持久化对象列表是否不为空
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                UserGroupVO vo = new UserGroupVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        pageResult.setObjects(vos);
        return pageResult;
    }

    @Override
    public void saveUserGroup(UserGroupParam param) throws BusinessException {
        //验证用户信息，从threadLocal获取用户id
        AdminVO adminVO = adminServiceImpl.detail();
        //断言异常
        Asserts.isTrue(adminVO == null, new BusinessException("-2", "认证解析错误"));
        Integer adminId = adminVO.getId();
        //保存用户人群信息
        UserGroupPO po = new UserGroupPO();
        BeanUtils.copyProperties(param, po);
        po.setBusinessId(adminVO.getBusinessId());
        po.setCreateAt(new Date());
        log.debug("saveUserGroup param:{}", param);
        userGroupRepository.save(po);
    }

    @Override
    public List<Integer> getGroupIds(List<Integer> tagIds, Integer businessId) {
        List<Integer> groupIds = new ArrayList<>();
        //匹配标签
        groupIds = userGroupRepository.getGroupIds(tagIds, businessId);
        return groupIds;
    }

    @Override
    public UserGroupVO detail(Long id) throws BusinessException {
        //根据id查询用户人群信息
        UserGroupPO po = userGroupRepository.findById(id);
        log.debug("查询结果 po:{}", po);
        //断言异常
        Asserts.isTrue(po == null, new BusinessException("-2", "用户人群不存在"));
        //将持久化对象转换为视图对象
        UserGroupVO vo = new UserGroupVO();
        BeanUtils.copyProperties(po, vo);
        log.debug("封装结果 vo:{}", vo);
        return vo;
    }
}
